//
//  OrderApi.swift
//  FeastBooking
//
//  Created by Imon on 2017/10/19.
//  Copyright © 2017年 Imon. All rights reserved.
//

import Foundation
import Moya

enum OrderApi {
    case postOrder(order: OrderPost)
    case postOrderContract(id: Int, deleted: String, contract: [UIImage])
    case updateOrderManager(id: Int, manager: String)
    case updateFeastingDate(id: Int, date: String)
    case postOrderPayment(id: Int, amountPaid: Double, deleted: String, payment: [UIImage])
    case getUserOrders(status: String, page: Int)
    case getOrderDetail(id: Int)
    case postOrderReviews(id: Int, rating: Int, review: String)
}

extension OrderApi: TargetType {
    
    var baseURL: URL {
        return URL(string: serverBasePath)!
    }
    
    var path: String {
        switch self {
        case .postOrder:
            return "/orders"
        case .postOrderContract(let id, _, _):
            return "/orders/\(id)/contract"
        case .updateOrderManager(let id, _):
            return "/orders/\(id)/manager"
        case .updateFeastingDate(let id, _):
            return "/orders/\(id)/feasting_date"
        case .postOrderPayment(let id, _, _, _):
            return "/orders/\(id)/payment"
        case .getUserOrders:
            return "/user/orders"
        case .getOrderDetail(let id):
            return "/orders/\(id)"
        case .postOrderReviews(let id, _, _):
            return "orders/\(id)/reviews"
        }
    }
    
    var method: Moya.Method {
        switch self {
        case .postOrder, .postOrderContract, .postOrderPayment, .postOrderReviews:
            return .post
        case .updateOrderManager, .updateFeastingDate:
            return .put
        case .getUserOrders, .getOrderDetail:
            return .get
        }
    }
    
    var parameters: [String: Any]? {
        switch self {
        case .postOrder(let order):
            return ["banquetHallId": order.banquetHallId!, "candidateDates": order.candidateDates!, "contact": order.contact!, "phone": order.phone!, "recommender": order.recommender!, "tables": order.tables!, "totp": order.totp!]
        case .postOrderContract, .postOrderPayment, .getOrderDetail:
            return nil            
        case .updateOrderManager(_, let manager):
            return ["username": manager]
        case .updateFeastingDate(_, let date):
            return ["value": date]
        case .getUserOrders(let status, let page):
            return ["status": status, "page": page]
        case .postOrderReviews(_, let rating, let review):
            return ["rating": rating, "review": review]
        }
    }
    
    var task: Task {
        switch self {
        case .postOrder, .updateOrderManager, .updateFeastingDate, .getUserOrders, .getOrderDetail, .postOrderReviews:
            return .request
        case .postOrderContract(_, let deleted, let contract):
            let form: [MultipartFormData] = buildFormDataFromContract(deleted: deleted, contract: contract)
            return .upload(.multipart(form))
        case .postOrderPayment(_, let amountPaid, let deleted, let payment):
            let form: [MultipartFormData] = buildFormDataFromPayment(amountPaid: amountPaid, deleted: deleted, payment: payment)
            return .upload(.multipart(form))
        }
    }
    
    var sampleData: Data {
        return "{\"data\":{\"msg\":\"good\"},\"meta\":{\"status\":200,\"msg\":\"OK\"}}".data(using: String.Encoding.utf8)!
    }
    
    var parameterEncoding: ParameterEncoding {
        switch self {
        case .postOrder, .postOrderReviews:
            return JSONEncoding.default
        case .updateOrderManager, .updateFeastingDate:
            return URLEncoding.queryString
        default:
            return URLEncoding.default
        }
    }
}

func buildFormDataFromContract(deleted: String, contract: [UIImage]) -> [MultipartFormData] {
    var result: [MultipartFormData] = []
    result.append(MultipartFormData(provider: .data(deleted.data(using: String.Encoding.utf8)!), name: "deleted"))
    for image in contract {
        let imageData: Data = image.compressImage(image: image, maxLength: imageMaxSize)!
        result.append(MultipartFormData(provider: .data(imageData), name: "files", fileName: "contract.jpg", mimeType: "image/jpeg"))
    }
    return result
}

func buildFormDataFromPayment(amountPaid: Double, deleted: String, payment: [UIImage]) -> [MultipartFormData] {
    var result: [MultipartFormData] = []
    result.append(MultipartFormData(provider: .data(String(amountPaid).data(using: String.Encoding.utf8)!), name: "amountPaid"))
    result.append(MultipartFormData(provider: .data(deleted.data(using: String.Encoding.utf8)!), name: "deleted"))
    for image in payment {
        let imageData: Data = image.compressImage(image: image, maxLength: imageMaxSize)!
        result.append(MultipartFormData(provider: .data(imageData), name: "files", fileName: "payment.jpg", mimeType: "image/jpeg"))
    }
    return result
}
